<template>
  <div class='ebook'>
    <title-bar :ifTitleAndMenuShow="ifTitleAndMenuShow" ref="titleBar"></title-bar>
    <div class="read-wrapper">
      <div id="read"></div>
      <div class="mask">
        <div class="left" @click="prevPage"></div>
        <div class="center" @click="toggleTitleAndMenu"></div>
        <div class="right" @click="nextPage"></div>
      </div>
    </div>
    <menu-bar ref="menuBar" :ifTitleAndMenuShow="ifTitleAndMenuShow" :fontSizeList="fontSizeList" :defaultFontSize="defaultFontSize" @setFontSize="setFontSize"
     :defaultTheme="defaultTheme" :themeList="themeList" @setTheme="setTheme" :bookAvailable="bookAvailable" @onProgressChange="onProgressChange"
      :navigation="navigation" @jumpTo="jumpTo"></menu-bar>
  </div>
</template>

<script>
import TitleBar from '@/components/TitleBar'
import MenuBar from '@/components/MenuBar'
import Epub from 'epubjs'
const DOWNLOAD_URL = "/static/Don't_lie_to_me_practise_a_pair_of_accurate_eyes.epub"
global.ePub = Epub
export default {
  components: {
    TitleBar,
    MenuBar
  },
  name: 'ebook',
  data () {
    return {
      ifTitleAndMenuShow: false,
      fontSizeList: [
        { fontSize: 12 },
        { fontSize: 14 },
        { fontSize: 16 },
        { fontSize: 18 },
        { fontSize: 20 },
        { fontSize: 24 }
      ],
      defaultFontSize: 16,
      themeList: [
        { name: 'default', style: { body: { 'color': '#000', 'background': '#fff' } } },
        { name: 'eye', style: { body: { 'color': '#000', 'background': '#ceeaba' } } },
        { name: 'night', style: { body: { 'color': '#fff', 'background': '#000' } } },
        { name: 'gold', style: { body: { 'color': '#000', 'background': 'rgba(241, 236, 226)' } } }
      ],
      defaultTheme: 0,
      // 图书是否处于可用状态
      bookAvailable: false,
      navigation: {}
    }
  },
  mounted () {
    this.showEpub()
  },
  methods: {
    // 电子书的解析和渲染
    showEpub () {
      // 生成Book
      this.book = new Epub(DOWNLOAD_URL)
      // 生成Rendition,通过Book.renderTo
      this.rendition = this.book.renderTo('read', {
        width: window.innerWidth,
        height: window.innerHeight
      })
      // 通过Rendition.display渲染电子书
      this.rendition.display()
      // 获取Theme对象
      this.themes = this.rendition.themes
      // 设置默认字体
      this.themes.fontSize(this.defaultFontSize + 'px')
      // 注册主题
      this.registerTheme()
      // 主题切换
      this.setTheme(this.defaultTheme)
      // 获取Locations对象
      // 通过epubjs的钩子函数来实现
      this.book.ready.then(() => {
        this.navigation = this.book.navigation
        return this.book.locations.generate()
      }).then(result => {
        this.locations = this.book.locations
        this.bookAvailable = true
      })
    },
    prevPage () {
      if (this.rendition) {
        this.rendition.prev()
      }
      this.hideTtleAndMenu()
    },
    nextPage () {
      if (this.rendition) {
        this.rendition.next()
      }
      this.hideTtleAndMenu()
    },
    toggleTitleAndMenu () {
      this.ifTitleAndMenuShow = !this.ifTitleAndMenuShow
      if (!this.ifTitleAndMenuShow) {
        this.$refs.menuBar.hideSetting()
      }
    },
    setFontSize (fontSize) {
      this.defaultFontSize = fontSize
      if (this.themes) {
        this.themes.fontSize(fontSize + 'px')
      }
    },
    registerTheme () {
      this.themeList.forEach(theme => {
        this.themes.register(theme.name, theme.style)
      })
    },
    setTheme (index) {
      this.themes.select(this.themeList[index].name)
      this.defaultTheme = index
    },
    // progress 进度条的数值(0-100)
    onProgressChange (progress) {
      const percentage = progress / 100
      const location = percentage > 0 ? this.locations.cfiFromPercentage(percentage) : 0
      this.rendition.display(location)
    },
    // 根据连接跳转到指定位置
    jumpTo (href) {
      this.rendition.display(href)
      this.hideTtleAndMenu()
    },
    hideTtleAndMenu () {
      this.ifTitleAndMenuShow = false
      this.$refs.menuBar.hideSetting()
      this.$refs.menuBar.hideContent()
    }
  }
}
</script>

<style lang='scss' scoped>
@import '../assets/styles/global.scss';
.ebook {
  position: relative;
  .read-wrapper {
    .mask {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 100;
      display: flex;
      .left {
        flex: 0 0 px2rem(100);
      }
      .center {
        flex: 1;
      }
      .right {
        flex: 0 0 px2rem(100);
      }
    }
  }
}
</style>
